package com.hzqy.web.interceptor;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.joda.time.Instant;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;
import com.hzqy.persistence.dao.content.ContentDao;
import com.hzqy.persistence.dao.hotel.WchatHotelDao;
import com.hzqy.web.vo.CmCatalogContentVo;
import com.hzqy.web.vo.CmContentVo;
import com.hzqy.web.vo.PmsOrgVo;
import com.hzqy.web.vo.PmsUserVo;

/**
 * 酒店微信公众号拦截器，只拦截酒店微型公众号中的action请求
 * 
 * @author Administrator
 *
 */
public class HotelWchatProcessLogInterceptor implements HandlerInterceptor {
	protected final Logger LOG = LoggerFactory.getLogger(getClass());
	
	private static final int PROCESS_CATEGORY_ID = 168;//微信操作日志分类id
	
	private static Map<Integer, String> CATEGORY_MAP = new HashMap<Integer, String>();
	
	private static final DateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
	
	
	
	static {
		CATEGORY_MAP.put(3, "酒店介绍");
		CATEGORY_MAP.put(4, "酒店服务");
		CATEGORY_MAP.put(5, "酒店美图");
		CATEGORY_MAP.put(14, "滚动条");
		CATEGORY_MAP.put(61, "酒店logo");
		CATEGORY_MAP.put(62, "酒店二维码");
		CATEGORY_MAP.put(86, "开机图片");
		CATEGORY_MAP.put(437, "酒店信息推送");

		CATEGORY_MAP.put(463, "背景大图");
		CATEGORY_MAP.put(270, "屏保图片");
		CATEGORY_MAP.put(269, "推荐位图片");
		CATEGORY_MAP.put(883, "客房服务");
	}
	
	@Resource
	private ContentDao contentDao;

	@Resource
	private WchatHotelDao wchatHotelDao;
	
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
		String uri = request.getRequestURI();
		PmsUserVo userVo = (PmsUserVo) request.getSession().getAttribute("pmsUserVo");
		PmsOrgVo orgVo = (PmsOrgVo) request.getSession().getAttribute("pmsOrgVo");
		String str = uri.substring(uri.lastIndexOf("/") + 1, uri.indexOf(".do") + 3);
		LOG.debug(str);
		
		//必须是业主身份
		if(userVo != null && orgVo != null && "2".equals(userVo.getF_pu_phone())) {
			CmContentVo processVo = new CmContentVo();
			processVo.setF_cc_companyid(orgVo.getF_po_id()+"");
			processVo.setF_cc_code(String.valueOf(userVo.getF_pu_id()));
			processVo.setF_cc_tags(userVo.getF_pu_name());
			processVo.setF_cc_status(1);
			processVo.setF_cc_onlinedate("2017-01-01 00:00:00");
			processVo.setF_cc_offlinedate("2099-12-31 23:59:59");
			processVo.setF_cc_offlinedate("2099-12-31 23:59:59");;
			processVo.setF_cc_end(0);
			processVo.setF_cc_seriescount(0);
			processVo.setF_cc_type(110);
			
			
			String catalogid_str = request.getParameter("catalogid");
			
			String operate = "修改";
			boolean save = true;
			
			Map<String, Object> msgMap = new HashMap<String, Object>();
			if("update_org_model.do".equals(str)) {
				//修改模版
				msgMap.put("title", "切换酒店模版");
				msgMap.put("success", "成功");
				
			} else if("update_content.do".equals(str) || "insertContent.do".equals(str)) {
				
				if("insertContent.do".equals(str)) {
					operate = "新增";
				} else {
					String offlinedate_str = request.getParameter("f_cc_offlinedate");
					if(StringUtils.isNotEmpty(offlinedate_str)) {
						Instant instant = Instant.parse(offlinedate_str);
						if(instant.compareTo(Instant.now())<0){
							operate = "下线";
						}else {
							operate = "上线";
						}
						/*Date offlinedate = DATE_FORMAT.parse(offlinedate_str);
						Date now = new Date();
						if(offlinedate.before(now)) {
							operate = "下线";
						} else {
							operate = "上线";
						}*/
					}
				}
				msgMap.put("title", CATEGORY_MAP.get(Integer.parseInt(catalogid_str)) + operate);
				msgMap.put("success", "成功");
				
			} else if("update_content_pic.do".equals(str)) {
				operate = "上传";
				msgMap.put("title", CATEGORY_MAP.get(Integer.parseInt(catalogid_str)) + operate);
				msgMap.put("success", "成功");
				
			} else if("delete_content.do".equals(str)) {
				operate = "删除";
				msgMap.put("title", CATEGORY_MAP.get(Integer.parseInt(catalogid_str)) + operate);
				msgMap.put("success", "成功");
			} else {
				save = false;
			}
			
			if(save) {
				processVo.setF_cc_name(JSONObject.toJSONString(msgMap));
				processVo.setF_cc_programatype("1");
				contentDao.insertContent(processVo);
				contentDao.insertContentOrg(processVo.getF_cc_id(),processVo.getF_cc_companyid());
				
				CmCatalogContentVo catalogContentVo = new CmCatalogContentVo();
				catalogContentVo.setF_ccc_contentid(processVo.getF_cc_id());
				catalogContentVo.setF_ccc_catalogid(PROCESS_CATEGORY_ID);
				wchatHotelDao.insertBind(catalogContentVo);
				
				LOG.debug("酒店日志拦截器 获取操作：" + processVo.getF_cc_name());
			}
		}
		return true;
	}

	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

	}

	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
	}
}
